package com.hxkcloud.patient.activitys;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.adapter.ChooseUserAdapter;
import com.hxkcloud.patient.adapter.FilterListAdapter;
import com.hxkcloud.patient.beans.DepartmentBean;
import com.hxkcloud.patient.beans.FilterBean;
import com.hxkcloud.patient.beans.FilterItem;
import com.hxkcloud.patient.beans.FilterListBean;
import com.hxkcloud.patient.beans.FilterPatientBean;
import com.hxkcloud.patient.beans.UserManagerBean;
import com.hxkcloud.patient.beans.UserManagerInfo;
import com.hxkcloud.patient.beans.resultbeans.ObjectResultBean;
import com.hxkcloud.patient.dborm.DepartmentDao;
import com.hxkcloud.patient.request.MessageCode;
import com.hxkcloud.patient.request.YiJiaHuNetWorkUtils;
import com.hxkcloud.patient.views.pulltorefreshlayout.PullToRefreshLayout;
import com.hxkcloud.patient.views.pulltorefreshlayout.PullableRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ray_lei on 2016/7/11.
 */
public class ChooseUserActivity extends BaseActivity implements View.OnClickListener, ChooseUserAdapter.ResultSelectedListener, PullToRefreshLayout.OnRefreshListener {

    private LinearLayout ll_title;
    private TextView tv_title;
    private ImageView iv_back;
    private TextView tv_finish;

    private LinearLayout ivgLayout;//导航布局

    private LinearLayout display1Layout;
    private LinearLayout display2Layout;
    private LinearLayout display3Layout;
    private LinearLayout display4Layout;
    private TextView display1TV;
    private TextView display2TV;
    private TextView display3TV;
    private TextView display4TV;
    private ImageView display1IV;
    private ImageView display2IV;
    private ImageView display3IV;
    private ImageView display4IV;
    private View middleView1;
    private View middleView2;
    private View middleView3;

    private PopupWindow allPopup;//选择全部
    private ListView allListView;
    private FilterListAdapter allAdapter;

    private View allWholeView;


    private PullToRefreshLayout refreshLayout;
    private PullableRecyclerView recycleView;

    private List<UserManagerBean> userManagerInfoList;
    private ChooseUserAdapter adapter;

    private Button allSelectBtn;
    private boolean isAllSelect = false;

    private int pageIndex = 1;
    private int pageSize = 15;

    private boolean isLoadMore = false;// 是否加载更多

    private ObjectResultBean<UserManagerInfo> resultBean;

    private int totalCount = 0;

    private String statusName = "";

    private boolean isAllClick = false;

    private List<FilterItem> filterItems;

    private ObjectResultBean<FilterListBean> filterBean;

    private List<FilterBean> listBeans;

    private boolean isDisplay1 = false;
    private boolean isDisplay2 = false;
    private boolean isDisplay3 = false;
    private boolean isDisplay4 = false;

    private int clickNum = 1;//1 2 3

    private String display1Default = "";
    private String display2Default = "";
    private String display3Default = "";
    private String display4Default = "";
    private String display1Id = "";
    private String display2Id = "";
    private String display3Id = "";
    private String display4Id = "";
    private String display1Type = "";
    private String display2Type = "";
    private String display3Type = "";
    private String display4Type = "";

    private List<FilterPatientBean> filterList;//筛选条件集合


    private String isNeedAll = "1";//0是 1否

    private boolean isChanke = false;

    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {
            case MessageCode.PREGNANTBMIADD_GETPATIENTFILTERNEWLIST_SUCCESS:
                dismisssLoading();
                if (msg.obj != null) {
                    resultBean = (ObjectResultBean<UserManagerInfo>) msg.obj;
                    totalCount = Integer.parseInt(resultBean.getData().getTotalCount());
                    if (resultBean.getData().getList().size() == 0) {
                        if (isLoadMore) {
                            refreshLayout
                                    .loadmoreFinish(PullToRefreshLayout.SUCCEED);
                        } else {
                            userManagerInfoList.clear();
                            adapter.setData(userManagerInfoList);
                            refreshLayout
                                    .refreshFinish(PullToRefreshLayout.SUCCEED);
                        }
                    } else {
                        if (isLoadMore) {

                        } else {
                            userManagerInfoList.clear();
                        }
                        userManagerInfoList.addAll(resultBean.getData().getList());
                        adapter.setData(userManagerInfoList);
                        if (isLoadMore) {
                            refreshLayout
                                    .loadmoreFinish(PullToRefreshLayout.SUCCEED);
                        } else {
                            refreshLayout
                                    .refreshFinish(PullToRefreshLayout.SUCCEED);
                        }

                    }

                }

                break;
            case MessageCode.PREGNANTBMIADD_GETPATIENTFILTERNEWLIST_FAILURE:
                dismisssLoading();
                if (msg.obj != null) {
                    toastMsg(msg.obj.toString());
                    return;
                }
                break;

            case MessageCode.PREGNANTBMIADD_GETPATIENTFILTERLIST_SUCCESS:
                dismisssLoading();
                if(msg.obj != null){
                    ivgLayout.setVisibility(View.VISIBLE);
                    filterBean = (ObjectResultBean<FilterListBean>)msg.obj;
                    listBeans = filterBean.getData().getList();
                    if(listBeans!=null && listBeans.size()>0){
                        for(int j = 0;j < listBeans.size();j++){
                            if("1".equals(listBeans.get(j).getFilterType())){
                                listBeans.remove(j);
                            }
                        }

                        if(listBeans.size() == 1){
                            display1Layout.setVisibility(View.VISIBLE);
                            display2Layout.setVisibility(View.GONE);
                            display3Layout.setVisibility(View.GONE);
                            display4Layout.setVisibility(View.GONE);
                            middleView1.setVisibility(View.GONE);
                            middleView2.setVisibility(View.GONE);
                            middleView3.setVisibility(View.GONE);
                        }else if(listBeans.size() == 2){
                            display1Layout.setVisibility(View.VISIBLE);
                            display2Layout.setVisibility(View.VISIBLE);
                            display3Layout.setVisibility(View.GONE);
                            display4Layout.setVisibility(View.GONE);
                            middleView2.setVisibility(View.GONE);
                            middleView3.setVisibility(View.GONE);
                        }else if(listBeans.size() == 3){
                            display1Layout.setVisibility(View.VISIBLE);
                            display2Layout.setVisibility(View.VISIBLE);
                            display3Layout.setVisibility(View.VISIBLE);
                            display4Layout.setVisibility(View.GONE);
                            middleView3.setVisibility(View.GONE);
                        }else if(listBeans.size() == 4){
                            display1Layout.setVisibility(View.VISIBLE);
                            display2Layout.setVisibility(View.VISIBLE);
                            display3Layout.setVisibility(View.VISIBLE);
                            display4Layout.setVisibility(View.VISIBLE);
                        }
                        for(int i = 0;i<listBeans.size();i++){
                            if(i == 0){
                                display1Default = listBeans.get(i).getFilterName();
                                display1Id = listBeans.get(i).getFilterId();
                                display1Type = listBeans.get(i).getFilterType();
                                if("1".equals(listBeans.get(i).getFilterType())){//时间
                                    display1TV.setText(display1Default);
                                }else if("2".equals(listBeans.get(i).getFilterType())){
                                    display1TV.setText(listBeans.get(i).getFilterList().get(0).getItemName());
                                    if(isChanke){

                                    }else{
                                        for(int j = 0;j<listBeans.get(i).getFilterList().size();j++){
                                            if(mContext.getResources().getString(R.string.inhospital).equals(listBeans.get(i).getFilterList().get(j).getItemName())){
                                                display1TV.setText(listBeans.get(i).getFilterList().get(j).getItemName());
                                                removeFilterListItem(display1Id);
                                                FilterPatientBean bean = new FilterPatientBean();
                                                bean.setDate("");
                                                bean.setDateKind("");
                                                bean.setFilterId(display1Id);
                                                bean.setFilterName(display1Default);
                                                bean.setFilterType(display1Type);
                                                bean.setItemId(listBeans.get(i).getFilterList().get(j).getItemId());
                                                filterList.add(bean);
                                                break;
                                            }
                                        }
                                    }

                                } else if("5".equals(listBeans.get(i).getFilterType())){//自理能力
                                    display1TV.setText(mContext.getResources().getString(R.string.disability_level));
                                }else{
                                    display1TV.setText(listBeans.get(i).getFilterList().get(0).getItemName());
                                }
                            }else if(i == 1){
                                display2Default = listBeans.get(i).getFilterName();
                                display2Id = listBeans.get(i).getFilterId();
                                display2Type = listBeans.get(i).getFilterType();
                                if("1".equals(listBeans.get(i).getFilterType())){//时间
                                    display2TV.setText(display2Default);
                                }else if("2".equals(listBeans.get(i).getFilterType())){
                                    display2TV.setText(listBeans.get(i).getFilterList().get(0).getItemName());
                                    if(isChanke){

                                    }else{
                                        for(int j = 0;j<listBeans.get(i).getFilterList().size();j++){
                                            if(mContext.getResources().getString(R.string.inhospital).equals(listBeans.get(i).getFilterList().get(j).getItemName())){
                                                display2TV.setText(listBeans.get(i).getFilterList().get(j).getItemName());
                                                removeFilterListItem(display2Id);
                                                FilterPatientBean bean = new FilterPatientBean();
                                                bean.setDate("");
                                                bean.setDateKind("");
                                                bean.setFilterId(display2Id);
                                                bean.setFilterName(display2Default);
                                                bean.setFilterType(display2Type);
                                                bean.setItemId(listBeans.get(i).getFilterList().get(j).getItemId());
                                                filterList.add(bean);
                                                break;
                                            }
                                        }
                                    }

                                } else if("5".equals(listBeans.get(i).getFilterType())){//自理能力
                                    display2TV.setText(mContext.getResources().getString(R.string.disability_level));
                                }else{
                                    display2TV.setText(listBeans.get(i).getFilterList().get(0).getItemName());
                                }
                            }else if(i == 2){
                                display3Default = listBeans.get(i).getFilterName();
                                display3Id = listBeans.get(i).getFilterId();
                                display3Type = listBeans.get(i).getFilterType();
                                if("1".equals(listBeans.get(i).getFilterType())){//时间
                                    display3TV.setText(display3Default);
                                }else if("2".equals(listBeans.get(i).getFilterType())){
                                    display3TV.setText(listBeans.get(i).getFilterList().get(0).getItemName());
                                    if(isChanke){

                                    }else{
                                        for(int j = 0;j<listBeans.get(i).getFilterList().size();j++){
                                            if(mContext.getResources().getString(R.string.inhospital).equals(listBeans.get(i).getFilterList().get(j).getItemName())){
                                                display3TV.setText(listBeans.get(i).getFilterList().get(j).getItemName());
                                                removeFilterListItem(display3Id);
                                                FilterPatientBean bean = new FilterPatientBean();
                                                bean.setDate("");
                                                bean.setDateKind("");
                                                bean.setFilterId(display3Id);
                                                bean.setFilterName(display3Default);
                                                bean.setFilterType(display3Type);
                                                bean.setItemId(listBeans.get(i).getFilterList().get(j).getItemId());
                                                filterList.add(bean);
                                                break;
                                            }
                                        }
                                    }
                                } else if("5".equals(listBeans.get(i).getFilterType())){//自理能力
                                    display3TV.setText(mContext.getResources().getString(R.string.disability_level));
                                }else{
                                    display3TV.setText(listBeans.get(i).getFilterList().get(0).getItemName());
                                }
                            }else if (i == 3){
                                display4Default = listBeans.get(i).getFilterName();
                                display4Id = listBeans.get(i).getFilterId();
                                display4Type = listBeans.get(i).getFilterType();
                                if("1".equals(listBeans.get(i).getFilterType())){//时间
                                    display4TV.setText(display4Default);
                                }else if("2".equals(listBeans.get(i).getFilterType())){
                                    display4TV.setText(listBeans.get(i).getFilterList().get(0).getItemName());
                                    if(isChanke){

                                    }else{
                                        for(int j = 0;j<listBeans.get(i).getFilterList().size();j++){
                                            if(mContext.getResources().getString(R.string.inhospital).equals(listBeans.get(i).getFilterList().get(j).getItemName())){
                                                display4TV.setText(listBeans.get(i).getFilterList().get(j).getItemName());
                                                removeFilterListItem(display4Id);
                                                FilterPatientBean bean = new FilterPatientBean();
                                                bean.setDate("");
                                                bean.setDateKind("");
                                                bean.setFilterId(display4Id);
                                                bean.setFilterName(display4Default);
                                                bean.setFilterType(display4Type);
                                                bean.setItemId(listBeans.get(i).getFilterList().get(j).getItemId());
                                                filterList.add(bean);
                                                break;
                                            }
                                        }
                                    }
                                } else if("5".equals(listBeans.get(i).getFilterType())){//自理能力
                                    display4TV.setText(mContext.getResources().getString(R.string.disability_level));
                                }else{
                                    display4TV.setText(listBeans.get(i).getFilterList().get(0).getItemName());
                                }
                            }

                        }
                        loading();
                        YiJiaHuNetWorkUtils.getPatientFilterResultList(filterList,isNeedAll,"", pageIndex, pageSize, handler);
                    }else{
                        ivgLayout.setVisibility(View.GONE);
                        YiJiaHuNetWorkUtils.getPatientFilterResultList(filterList, isNeedAll, "", pageIndex, pageSize, handler);
                    }

                }
                break;
            case MessageCode.PREGNANTBMIADD_GETPATIENTFILTERLIST_FAILURE:

                break;

        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout_chooseuser);
        userManagerInfoList = new ArrayList<UserManagerBean>();
        listBeans = new ArrayList<>();
        filterItems = new ArrayList<>();
        filterList = new ArrayList<>();
        if(getIntent().hasExtra("isNeedAll")){
            isNeedAll = getIntent().getStringExtra("isNeedAll");
        }
        initView();
        initData();
        initListener();
    }

    @Override
    protected void initView() {

        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_title = (TextView) findViewById(R.id.tv_title_title);
        tv_finish = (TextView) findViewById(R.id.tv_title_func);

        setTitlePadding(ll_title);
        iv_back.setVisibility(View.VISIBLE);
        tv_title.setText(getResources().getText(R.string.chooseuser_text_title));
        tv_finish.setVisibility(View.VISIBLE);
        tv_finish.setText(getResources().getText(R.string.sure));
        iv_back.setOnClickListener(this);
        tv_finish.setOnClickListener(this);

        ivgLayout = (LinearLayout) findViewById(R.id.chooseuser_layout_ivg);

        display1Layout = (LinearLayout) findViewById(R.id.chooseuser_layout_display1);
        display2Layout = (LinearLayout) findViewById(R.id.chooseuser_layout_display2);
        display3Layout = (LinearLayout) findViewById(R.id.chooseuser_layout_display3);
        display4Layout = (LinearLayout) findViewById(R.id.chooseuser_layout_display4);

        display1TV = (TextView) findViewById(R.id.chooseuser_tv_display1);
        display2TV = (TextView) findViewById(R.id.chooseuser_tv_display2);
        display3TV = (TextView) findViewById(R.id.chooseuser_tv_display3);
        display4TV = (TextView) findViewById(R.id.chooseuser_tv_display4);

        display1IV = (ImageView) findViewById(R.id.chooseuser_iv_display1);
        display2IV = (ImageView) findViewById(R.id.chooseuser_iv_display2);
        display3IV = (ImageView) findViewById(R.id.chooseuser_iv_display3);
        display4IV = (ImageView) findViewById(R.id.chooseuser_iv_display4);

        middleView1 = (View) findViewById(R.id.chooseuser_view_view1);
        middleView2 = (View) findViewById(R.id.chooseuser_view_view2);
        middleView3 = (View) findViewById(R.id.chooseuser_view_view3);


        refreshLayout = (PullToRefreshLayout) findViewById(R.id.chooseuser_refreshlayout);
        recycleView = (PullableRecyclerView) findViewById(R.id.chooseuser_recyclerview);

        LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(mLayoutManager);
        adapter = new ChooseUserAdapter(this, userManagerInfoList, recycleView, this, screenWidth, loginInfo.getDepartmentId());
        recycleView.setAdapter(adapter);

        allSelectBtn = (Button) findViewById(R.id.chooseuser_btn_allselect);

        initAllPopup();


    }

    @Override
    protected void initListener() {
        iv_back.setOnClickListener(this);
        tv_finish.setOnClickListener(this);
        allSelectBtn.setOnClickListener(this);

        display1Layout.setOnClickListener(this);
        display2Layout.setOnClickListener(this);
        display3Layout.setOnClickListener(this);
        display4Layout.setOnClickListener(this);

        refreshLayout.setOnRefreshListener(this);
    }

    @Override
    protected void initData() {
        DepartmentDao dao = new DepartmentDao(mContext);
        List<DepartmentBean> beans = dao.getDepartment();
        if (beans != null) {
            for (int i = 0; i < beans.size(); i++) {
                if ((beans.get(i).getDepartmentId() + "").equals(loginInfo.getDepartmentId())) {
                    if ("0".equals(beans.get(i).getType())) {
                        isChanke = true;
                    }
                    break;
                }
            }
        }

        loading();
        YiJiaHuNetWorkUtils.getPatientFilterList(loginInfo.getDepartmentId(), handler);
        pageIndex = 1;
    }

    private void initAllPopup() {
        allPopup = new PopupWindow(this);
        View allView = LayoutInflater.from(this).inflate(R.layout.popup_layout_kind, null);
        allPopup.setContentView(allView);

        allPopup.setWidth(screenWidth);
        allPopup.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        allPopup.setFocusable(true);
        allPopup.setTouchable(true);
        allPopup.setOutsideTouchable(true);
        allPopup.setBackgroundDrawable(new ColorDrawable(0x00000000));


        allListView = (ListView) allView.findViewById(R.id.kind_listview);
        allAdapter = new FilterListAdapter(this, filterItems);
        allListView.setAdapter(allAdapter);
        allListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                for (int i = 0; i < filterItems.size(); i++) {
                    if (i == position) {
                        filterItems.get(i).setIsSelect(true);
                        statusName = filterItems.get(i).getItemName();
                    } else {
                        filterItems.get(i).setIsSelect(false);
                    }
                }
                FilterPatientBean bean = new FilterPatientBean();
                bean.setDate("");
                bean.setDateKind("");
                if (clickNum == 1) {
                    isDisplay1 = true;
//                    display1TV.setText(filterItems.get(position).getItemName());
                    String name = filterItems.get(position).getItemName();
                    if("5".equals(display1Type) && "-1".equals(filterItems.get(position).getItemId())){
                        display1TV.setText(mContext.getResources().getString(R.string.disability_level));
                    }else{
                        if (name.length() > 4) {
                            String displayName = name.substring(0, 3);
                            display1TV.setText(displayName + "...");
                        } else {
                            display1TV.setText(name);
                        }
                    }
                    display1IV.setImageResource(R.mipmap.tab_arrow);
                    removeFilterListItem(display1Id);
                    bean.setFilterId(display1Id);
                    bean.setFilterName(display1Default);
                    bean.setFilterType(display1Type);
                    bean.setItemId(filterItems.get(position).getItemId());

                } else if (clickNum == 2) {
                    isDisplay2 = true;
//                    display2TV.setText(filterItems.get(position).getItemName());
                    String name = filterItems.get(position).getItemName();
                    if("5".equals(display2Type) && "-1".equals(filterItems.get(position).getItemId())){
                        display2TV.setText(mContext.getResources().getString(R.string.disability_level));
                    }else{
                        if (name.length() > 4) {
                            String displayName = name.substring(0, 3);
                            display2TV.setText(displayName + "...");
                        } else {
                            display2TV.setText(name);
                        }
                    }
                    display2IV.setImageResource(R.mipmap.tab_arrow);
                    removeFilterListItem(display2Id);
                    bean.setFilterId(display2Id);
                    bean.setFilterName(display2Default);
                    bean.setFilterType(display2Type);
                    bean.setItemId(filterItems.get(position).getItemId());
                } else if (clickNum == 3) {
                    isDisplay3 = true;
//                    display3TV.setText(filterItems.get(position).getItemName());
                    String name = filterItems.get(position).getItemName();
                    if("5".equals(display3Type) && "-1".equals(filterItems.get(position).getItemId())){
                        display3TV.setText(mContext.getResources().getString(R.string.disability_level));
                    }else{
                        if (name.length() > 4) {
                            String displayName = name.substring(0, 3);
                            display3TV.setText(displayName + "...");
                        } else {
                            display3TV.setText(name);
                        }
                    }
                    display3IV.setImageResource(R.mipmap.tab_arrow);
                    removeFilterListItem(display3Id);
                    bean.setFilterId(display3Id);
                    bean.setFilterName(display3Default);
                    bean.setFilterType(display3Type);
                    bean.setItemId(filterItems.get(position).getItemId());
                }else if(clickNum == 4){
                    isDisplay4 = true;
//                    display4TV.setText(filterItems.get(position).getItemName());
                    String name = filterItems.get(position).getItemName();
                    if("5".equals(display4Type) && "-1".equals(filterItems.get(position).getItemId())){
                        display4TV.setText(mContext.getResources().getString(R.string.disability_level));
                    }else{
                        if (name.length() > 4) {
                            String displayName = name.substring(0, 3);
                            display4TV.setText(displayName + "...");
                        } else {
                            display4TV.setText(name);
                        }
                    }
                    display4IV.setImageResource(R.mipmap.tab_arrow);
                    removeFilterListItem(display4Id);
                    bean.setFilterId(display4Id);
                    bean.setFilterName(display4Default);
                    bean.setFilterType(display4Type);
                    bean.setItemId(filterItems.get(position).getItemId());
                }
                filterList.add(bean);

                allAdapter.notifyDataSetChanged();
                allPopup.dismiss();
                loading();
                YiJiaHuNetWorkUtils.getPatientFilterResultList(filterList,isNeedAll, "", pageIndex, pageSize, handler);

            }
        });
        allAdapter.setDisplayListener(new FilterListAdapter.DisplayListener() {
            @Override
            public void onDisplay(int position) {

            }
        });

        allWholeView = (View) allView.findViewById(R.id.kind_view_whole);
        allWholeView.setOnClickListener(this);

        allPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                display1IV.setImageResource(R.mipmap.tab_arrow);
                display2IV.setImageResource(R.mipmap.tab_arrow);
                display3IV.setImageResource(R.mipmap.tab_arrow);
                display4IV.setImageResource(R.mipmap.tab_arrow);
            }
        });
    }

    private void setInitPopup() {

        display1TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
        display1IV.setImageResource(R.mipmap.tab_arrow);


        display2TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
        display2IV.setImageResource(R.mipmap.tab_arrow);

        display3TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
        display3IV.setImageResource(R.mipmap.tab_arrow);

        display4TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
        display4IV.setImageResource(R.mipmap.tab_arrow);


    }

    private void removeFilterListItem(String kindId){
        if(filterList!=null && filterList.size()>0){
            for(int i = 0;i < filterList.size();i++){
                if(kindId.equals(filterList.get(i).getFilterId())){
                    filterList.remove(i);
                }
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.all_view_whole:
                if (allPopup != null) {
                    allPopup.dismiss();
                }
                break;
            case R.id.kind_view_whole:
                display1IV.setImageResource(R.mipmap.tab_arrow);
                display2IV.setImageResource(R.mipmap.tab_arrow);
                display3IV.setImageResource(R.mipmap.tab_arrow);
                allPopup.dismiss();

                break;
            case R.id.chooseuser_layout_display1:
                setInitPopup();
                clickNum =1;
                display1IV.setImageResource(R.mipmap.tab_arrow_up);
                if(listBeans != null && listBeans.size()>0){
                    for(int i = 0;i<listBeans.size();i++){
                        if(i == 0){
                            filterItems = listBeans.get(i).getFilterList();
                            if(filterItems.size() > 0){
                                if(isDisplay1){

                                }else{
                                    if(isChanke){
                                        filterItems.get(0).setIsSelect(true);
                                    }else{
                                        for(int j = 0;j<filterItems.size();j++){
                                            if(mContext.getResources().getString(R.string.inhospital).equals(filterItems.get(j).getItemName())){
                                                filterItems.get(0).setIsSelect(false);
                                                filterItems.get(j).setIsSelect(true);
                                                break;
                                            }else{
                                                filterItems.get(0).setIsSelect(true);
                                            }
                                        }
                                    }

                                }
                            }
                            break;
                        }
                    }
                }
                if(filterItems != null && filterItems.size() > 0){
                    allAdapter.setData(filterItems);
                    allPopup.showAsDropDown(ivgLayout);
                }
                break;
            case R.id.chooseuser_layout_display2:
                setInitPopup();
                clickNum =2;
                display2IV.setImageResource(R.mipmap.tab_arrow_up);
                if(listBeans != null && listBeans.size()>0){
                    for(int i = 0;i<listBeans.size();i++){
                        if(i == 1){
                            filterItems = listBeans.get(i).getFilterList();
                            if(filterItems.size() > 0){
                                if(isDisplay2){

                                }else{
                                    if(isChanke){
                                        filterItems.get(0).setIsSelect(true);
                                    }else{
                                        for(int j = 0;j<filterItems.size();j++){
                                            if(mContext.getResources().getString(R.string.inhospital).equals(filterItems.get(j).getItemName())){
                                                filterItems.get(0).setIsSelect(false);
                                                filterItems.get(j).setIsSelect(true);
                                                break;
                                            }else{
                                                filterItems.get(0).setIsSelect(true);
                                            }
                                        }
                                    }
                                }

                            }
                            break;
                        }
                    }
                }
                if(filterItems != null && filterItems.size() > 0){
                    allAdapter.setData(filterItems);
                    allPopup.showAsDropDown(ivgLayout);
                }

                break;
            case R.id.chooseuser_layout_display3:

                setInitPopup();
                clickNum =3;
                display3IV.setImageResource(R.mipmap.tab_arrow_up);
                if(listBeans != null && listBeans.size()>0){
                    for(int i = 0;i<listBeans.size();i++){
                        if(i == 2){
                            filterItems = listBeans.get(i).getFilterList();
                            if(filterItems.size() > 0){
                                if(isDisplay3){

                                }else{
                                    if(isChanke){
                                        filterItems.get(0).setIsSelect(true);
                                    }else{
                                        for(int j = 0;j<filterItems.size();j++){
                                            if(mContext.getResources().getString(R.string.inhospital).equals(filterItems.get(j).getItemName())){
                                                filterItems.get(0).setIsSelect(false);
                                                filterItems.get(j).setIsSelect(true);
                                                break;
                                            }else{
                                                filterItems.get(0).setIsSelect(true);
                                            }
                                        }
                                    }
                                }
                            }
                            break;
                        }
                    }
                }
                if(filterItems != null && filterItems.size() > 0){
                    allAdapter.setData(filterItems);
                    allPopup.showAsDropDown(ivgLayout);
                }
                break;

            case R.id.chooseuser_layout_display4:

                setInitPopup();
                clickNum =4;
                display4IV.setImageResource(R.mipmap.tab_arrow_up);
                if(listBeans != null && listBeans.size()>0){
                    for(int i = 0;i<listBeans.size();i++){
                        if(i == 3){
                            filterItems = listBeans.get(i).getFilterList();
                            if(filterItems.size() > 0){
                                if(isDisplay4){

                                }else{
                                    if(isChanke){
                                        filterItems.get(0).setIsSelect(true);
                                    }else{
                                        for(int j = 0;j<filterItems.size();j++){
                                            if(mContext.getResources().getString(R.string.inhospital).equals(filterItems.get(j).getItemName())){
                                                filterItems.get(0).setIsSelect(false);
                                                filterItems.get(j).setIsSelect(true);
                                                break;
                                            }else{
                                                filterItems.get(0).setIsSelect(true);
                                            }
                                        }
                                    }
                                }
                            }
                            break;
                        }
                    }
                }
                if(filterItems != null && filterItems.size() > 0){
                    allAdapter.setData(filterItems);
                    allPopup.showAsDropDown(ivgLayout);
                }
                break;


            case R.id.iv_title_back:
                finish();
                break;

            case R.id.tv_title_func:
                ArrayList<String> patients = new ArrayList<>();
                for (UserManagerBean user : userManagerInfoList) {
                    if (user.isSelect()) {
                        patients.add(user.getPatientId());
                    }
                }
                if (patients.size() == 0) {
                    toastMsg(R.string.pleasechoosepatients);
                } else {
                    Intent iBack = new Intent();
                    iBack.putStringArrayListExtra("patients", patients);
                    setResult(RESULT_OK, iBack);
                    finish();
                }
                break;
            case R.id.chooseuser_btn_allselect://全选
                if (isAllSelect) {
                    allSelectBtn.setBackgroundResource(R.mipmap.btn_multiselect);
                    for (int i = 0; i < userManagerInfoList.size(); i++) {
                        userManagerInfoList.get(i).setIsSelect(false);
                    }
                    isAllSelect = false;
                } else {
                    allSelectBtn.setBackgroundResource(R.mipmap.btn_multiselect_pre);
                    for (int i = 0; i < userManagerInfoList.size(); i++) {
                        userManagerInfoList.get(i).setIsSelect(true);
                    }
                    isAllSelect = true;
                }
                adapter.notifyDataSetChanged();


                break;

        }
    }

    @Override
    public void onSelected(String type, int position) {

        if ("select".equals(type)) {
            userManagerInfoList.get(position).setIsSelect(true);
            adapter.notifyItemChanged(position);

            boolean isAllSelectS = true;
            for (UserManagerBean bean : userManagerInfoList) {
                if (!bean.isSelect()) {
                    isAllSelectS = false;
                }
            }

            if (isAllSelectS) {
                allSelectBtn.setBackgroundResource(R.mipmap.btn_multiselect_pre);
                isAllSelect = true;
            }
        } else if ("no_select".equals(type)) {
            userManagerInfoList.get(position).setIsSelect(false);
            adapter.notifyItemChanged(position);

            boolean isAllSelectNoS = false;
            for (UserManagerBean bean : userManagerInfoList) {
                if (!bean.isSelect()) {
                    isAllSelectNoS = true;
                }
            }

            if (isAllSelectNoS) {
                allSelectBtn.setBackgroundResource(R.mipmap.btn_multiselect);
                isAllSelect = false;
            }
        }
    }

    @Override
    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
        isLoadMore = false;
        pageIndex = 1;
        userManagerInfoList.clear();

        isAllSelect = false;
        allSelectBtn.setBackgroundResource((R.mipmap.btn_multiselect));

        YiJiaHuNetWorkUtils.getPatientFilterResultList(filterList, isNeedAll, "", pageIndex, pageSize, handler);
    }

    @Override
    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {

        if (userManagerInfoList.size() == totalCount) {
            isLoadMore = false;
            refreshLayout
                    .loadmoreFinish(PullToRefreshLayout.SUCCEED);
            return;
        }

        isAllSelect = false;
        allSelectBtn.setBackgroundResource((R.mipmap.btn_multiselect));

        isLoadMore = true;
        pageIndex++;
        YiJiaHuNetWorkUtils.getPatientFilterResultList(filterList, isNeedAll, "", pageIndex, pageSize, handler);

    }
}
